package leetcode.剑指offer.前40;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author :songyaozhang
 * @date :Created 2021/7/27 19:58
 * @description :https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
 */
public class 剑指_21_调整数组顺序使奇数位于偶数前面 {

    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.exchange(new int[]{1, 2, 3, 4})));

    }

    class Solution {
        public int[] exchange(int[] nums) {
            int l = 0;
            int r = nums.length - 1;
            while(l < r) {
                while (l < r && nums[l] % 2 == 1) {
                    l++;
                }
                while (l < r && (nums[r] % 2) == 0) {
                    r--;
                }
                if (l < r) {
                    int temp = nums[l];
                    nums[l] = nums[r];
                    nums[r] = temp;
                }
             }
            return nums;
        }
    }


}
